// 单击行列变色begin
$("#table2 tbody td").click(function () {
    var index = $(this).parents("tr").find("td").index($(this)) + 1;
    var indextr = $('#table2 tr').index($(this).parent("tr"));
    if (indextr === -1) {
        indextr = $('#table2_tableColumnClone tr').index($(this).parent("tr"))
    }
    $(this).parent().parent().children("tr").removeClass("borBlue");
    $("#table2_tableLayout tbody tr").removeClass("borBlue");
    $("#table2_tableLayout tbody td").removeClass("borBlue");
    $("#table2_tableLayout tbody td").removeClass("borRed");
    $(this).addClass("borRed");
    // $(this).parent().addClass("borBlue");
    $('#table2_tableLayout #table2 tr').eq(indextr).addClass("borBlue");
    $('#table2_tableLayout #table2_tableColumnClone tr').eq(indextr).addClass("borBlue");
    $("tr td:nth-child(" + index + ")").addClass("borBlue");
});
// 单击行列变色end
//图标淡入淡出效果begin
function show(str) {
    $('.data_success .str').text(str).fadeIn(0, function () {
        $(".data_success").css({
            "opacity": "1"
        })
    }).delay(3000).fadeOut(500, function () {
        $(".data_success").css({
            "opacity": "0"
        })
    });
}

//调整页面小数的显示位数
function reserveddecimal() {
    var decimaldigit = storage.get("decimaldigit");
    if (!decimaldigit) {
        decimaldigit = 6;
    } else if (decimaldigit["decimaldigit"] == 6) {
        decimaldigit = 2;
    } else {
        decimaldigit = 6;
    }
    storage.set("decimaldigit", {decimaldigit: decimaldigit});
    var tableName = decodeURIComponent(getUrlParms("tableName").split(",")[0]);//获取地址栏的参数并且编码转译
    var data1 = {
        decimaldigit: decimaldigit,//小数位数
        tableName: tableName//要修改的值是哪个部门
    };
    $.ajax({
        type: 'POST',
        url: "/editortable/reserveddecimal",
        data: data1,
        success: function (res) {
            if (res.Modification) {
                var result = res.result, k;
                for (var i in result) {
                    k = -2;
                    for (var j in result[i]) {
                        k++;
                        if (j === "id") continue;
                        $("#table2 tbody tr").eq(i).find("td").eq(k).text(result[i][j]);
                    }
                }
                return;
            }
            alert("数据修改失败,请输入正确的格式");
            document.getElementsByClassName("tdValuei")[0].focus();
        }
    });
}

//
//下载功能begin
// var tableId = document.getElementById('table2');
function toexcel() {
    var tableName = decodeURIComponent(getUrlParms("tableName").split(",")[0]);
    $("#table2").table2excel({
        exclude: ".noExl", //过滤位置的 css 类名
        filename: tableName + ".xls", //文件名称
        name: "Excel Document Name.xlsx",
        exclude_img: true,
        exclude_links: true,
        exclude_inputs: true
    });
}

//下载功能end
function saveLock() {//一键保存锁定方法
    var tableName = decodeURIComponent(getUrlParms("tableName").split(",")[0]);//获取地址栏的参数并且编码转译
    var saveResult = confirm("保存后不能继续修改,请确认是否保存")
    var data1 = {
        tableName: tableName,
        state: 2//状态改为2,可查看不能再修改
    }
    if (saveResult) {
        $.ajax({
            type: 'POST',
            url: "/editortable",
            data: data1,
            success: function (res) {
                if (res.saveSuc) {
                    window.location.href = "/";
                    return;
                }
                alert("保存失败");
            }
        });
    }

}

//点击按钮查看begin
function ViDaSou() {
    var dataFiled = storage.get("CellInfo");
    console.log(dataFiled);
    var hideobj = document.getElementById("hidebg");
    var strtwrap = document.getElementsByClassName("strt-wrap");
    hidebg.style.display = "block";  //显示隐藏层
    hidebg.style.height = document.body.clientHeight + "px";  //设置隐藏层的高度为当前页面高度
    strtwrap[0].style.width = "100%";
    $.ajax({
        type: "POST",
        async: false,//同步
        url: "/editortable/ViDaSou",
        data: dataFiled,
        success: function (data) {
            if (data.BasicData.indexOf("最底层") != -1) {
                console.log(data.BasicData)
                document.getElementById("hidebox").style.display = "block";  //显示弹出层
                $(".strt-wrap").css({
                    "display": "none"
                })
                $("#hidebox").children().remove();
                $("#hidebox").append("<div>" + data.BasicData + "</div>");
            } else {
                $(".strt-wrap").css({
                    "display": "block",
                    "position": "absolute",
                    "top": "25%"
                })
                $(".strt-block:first").css({
                    "display": "block",
                    "position": "relative",
                    "left": "50%",
                    "transform": "translateX(-50%)",
                })
                $(".Department").text("");
                $(".Department").append(dataFiled.tableName);
                $(".part-contenct").children().remove();
                for (var i = 0; i < data.BasicData.length; i++) {
                    if (i === 0) {

                        var strtpart = '<div class="strt-part">\n' +
                            '<span class="line-h line-h-r"></span>\n' +
                            '<div class="line-v"><span></span></div>\n' +
                            '<span class="strt-name">\n' +
                            '<p>' + data.BasicData[i].table_name.split("_")[0] + '</p>\n' +
                            '<p>' + data.BasicData[i]["valva"] + '</p>\n' +
                            '</span>\n' +
                            '</div>';
                    } else if (i === data.BasicData.length - 1) {
                        strtpart = '<div class="strt-part">\n' +
                            '<span class="line-h line-h-l"></span>\n' +
                            '<div class="line-v"><span></span></div>\n' +
                            '<span class="strt-name">\n' +
                            '<p>' + data.BasicData[i].table_name.split("_")[0] + '</p>\n' +
                            '<p>' + data.BasicData[i]["valva"] + '</p>\n' +
                            '</span>\n' +
                            '</div>';
                    } else {
                        strtpart = '<div class="strt-part">\n' +
                            '<span class="line-h line-h-c"></span>\n' +
                            '<div class="line-v"><span></span></div>\n' +
                            '<span class="strt-name">\n' +
                            '<p>' + data.BasicData[i].table_name.split("_")[0] + '</p>\n' +
                            '<p>' + data.BasicData[i]["valva"] + '</p>\n' +
                            '</span>\n' +
                            '</div>';
                    }
                    $(".part-contenct").append(strtpart);
                }
                console.log(data.BasicData)
            }
        }
    })
}

//重抓某个单元格的数据begin
function Regrasp() {
    var dataFiled = storage.get("CellInfo");
    console.log(dataFiled);
    var r = confirm("请确认是否抓取该单元格？");
    if (r) {
        $.ajax({
            type: "POST",
            async: false,//同步
            url: "/Singlecell/singleGrasp",
            data: dataFiled,
            success: function (data) {
                if (data["success"]) {
                    alert("任务创建成功,请稍后查看");
                } else {
                    alert("该单元格不是抓取项,不支持重抓");
                }
            }

        })
    }
}

// 点击按钮关闭begin
function hide()  //去除隐藏层和弹出层
{
    document.getElementById("hidebg").style.display = "none";
    document.getElementById("hidebox").style.display = "none";
}

function FixTable(TableID, FixColumnNumber, width, height) {
    //TableID            要锁定的Table的ID
    //FixColumnNumber    要锁定列的个数
    //width              显示的宽度
    //height             显示的高度
    if ($("#" + TableID + "_tableLayout").length != 0) {
        $("#" + TableID + "_tableLayout").before($("#" + TableID));
        $("#" + TableID + "_tableLayout").empty();
    } else {
        $("#" + TableID).after("<div id='" + TableID + "_tableLayout' style='overflow:hidden; width:" + width + "px;'></div>");
    }
    $('<div id="' + TableID + '_tableFix"></div>'
        + '<div id="' + TableID + '_tableHead"></div>'
        + '<div id="' + TableID + '_tableColumn"></div>'
        + '<div id="' + TableID + '_tableData"></div>').appendTo("#" + TableID + "_tableLayout");
    var oldtable = $("#" + TableID);
    var tableFixClone = oldtable.clone(true);
    tableFixClone.attr("id", TableID + "_tableFixClone");
    $("#" + TableID + "_tableFix").append(tableFixClone);
    var tableHeadClone = oldtable.clone(true);
    tableHeadClone.attr("id", TableID + "_tableHeadClone");
    $("#" + TableID + "_tableHead").append(tableHeadClone);
    var tableColumnClone = oldtable.clone(true);
    tableColumnClone.attr("id", TableID + "_tableColumnClone");
    $("#" + TableID + "_tableColumn").append(tableColumnClone);
    $("#" + TableID + "_tableData").append(oldtable);
    $("#" + TableID + "_tableLayout table").each(function () {
        $(this).css("margin", "0");
    });
    var HeadHeight = $("#" + TableID + "_tableHead thead").height();
    HeadHeight += 2;
    $("#" + TableID + "_tableHead").css("height", HeadHeight);
    $("#" + TableID + "_tableFix").css("height", HeadHeight);
    var ColumnsWidth = 0;
    var ColumnsNumber = 0;
    $("#" + TableID + "_tableColumn tr:last td:lt(" + FixColumnNumber + ")").each(function () {
        ColumnsWidth += $(this).outerWidth(true);
        ColumnsNumber++;
    });
    ColumnsWidth += 2;
    if ($.browser.msie) {
        switch ($.browser.version) {
            case "7.0":
                if (ColumnsNumber >= 3) ColumnsWidth--;
                break;
            case "8.0":
                if (ColumnsNumber >= 2) ColumnsWidth--;
                break;
        }
    }
    $("#" + TableID + "_tableColumn").css("width", ColumnsWidth);
    $("#" + TableID + "_tableFix").css("width", ColumnsWidth);
    $("#" + TableID + "_tableData").scroll(function () {
        $("#" + TableID + "_tableHead").scrollLeft($("#" + TableID + "_tableData").scrollLeft());
        $("#" + TableID + "_tableColumn").scrollTop($("#" + TableID + "_tableData").scrollTop());
    });
    $("#" + TableID + "_tableFix").css({
        "overflow": "hidden",
        "position": "relative",
        "z-index": "50",
        "background-color": "white"
    });
    $("#" + TableID + "_tableHead").css({
        "overflow": "hidden",
        "width": width - 17,
        "position": "relative",
        "z-index": "45",
        "background-color": "white"
    });
    $("#" + TableID + "_tableColumn").css({
        "overflow": "hidden",
        "height": height - 17,
        "position": "relative",
        "z-index": "40",
        "background-color": "white"
    });
    $("#" + TableID + "_tableData").css({
        "overflow": "scroll",
        "width": width,
        "height": height,
        "position": "relative",
        "z-index": "35"
    });
    if ($("#" + TableID + "_tableHead").width() > $("#" + TableID + "_tableFix table").width()) {
        $("#" + TableID + "_tableHead").css("width", $("#" + TableID + "_tableFix table").width());
        $("#" + TableID + "_tableData").css("width", $("#" + TableID + "_tableFix table").width() + 17);
    }
    if ($("#" + TableID + "_tableColumn").height() > $("#" + TableID + "_tableColumn table").height()) {
        $("#" + TableID + "_tableColumn").css("height", $("#" + TableID + "_tableColumn table").height());
        $("#" + TableID + "_tableData").css("height", $("#" + TableID + "_tableColumn table").height() + 17);
    }
    $("#" + TableID + "_tableFix").offset($("#" + TableID + "_tableLayout").offset());
    $("#" + TableID + "_tableHead").offset($("#" + TableID + "_tableLayout").offset());
    $("#" + TableID + "_tableColumn").offset($("#" + TableID + "_tableLayout").offset());
    $("#" + TableID + "_tableData").offset($("#" + TableID + "_tableLayout").offset());
}

function getUrlParms(name) {//获取地址栏传递的参数
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

//判断是否为数字
function isRealNum(val) {
    // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
    if (val === "" || val == null) {
        return false;
    }
    if (!isNaN(val)) {
        return true;
    } else {
        return false;
    }
}

var tbwidth = document.body.clientWidth;
var tbheight = document.documentElement.clientHeight;
FixTable("table2", 2, tbwidth, tbheight);
$(document).ready(function () {
    storage.remove("decimaldigit");//刷新浏览器清除小数点的存储
    window.onresize = function () {
        tbwidth = document.body.clientWidth;
        tbheight = document.documentElement.clientHeight;
        FixTable("table2", 2, tbwidth, tbheight);
    };
    var setColor;
    //修改单元格的值
    // $("#table2 td").dblclick(function () {
    //     //     alert("该版本不支持线上修改,请下载后修改")
    //     // })
    $("#table2 td").dblclick(function () {
        if ($(".tdValuei")) {
            var valinput = $(".tdValuei").eq(0).val();
            $(".tdValuei").eq(0).parent().text(valinput);
            $(".tdValuei").eq(0).remove();
        }
        var tdvalue = $(this).text();
        $(this).text("");
        $(this).append('<input class="tdValuei" type="text" value=' + tdvalue + '>');
        document.getElementsByClassName("tdValuei")[0].focus();
        var lineNumber = $(this).parent("tr").children("td").eq(0).text();
        var Num = $(this).parents("tr").find("td").index($(this));
        var field = $("#table2>thead>tr").eq(6).children("th").eq(Num).text();
        var tableName = decodeURIComponent(getUrlParms("tableName").split(",")[0]);//获取地址栏的参数并且编码转译
        // var valnum = prompt("请输入修改的值");
        $(".tdValuei").eq(0).change(function () {
            var valnum = $(this).val();
            var data1 = {
                lineNumber: lineNumber,//行号
                field: field,//字段
                valnum: valnum,//修改的值
                tableName: tableName//要修改的值是哪个部门
            };
            if (isRealNum(valnum)) {
                $.ajax({
                    type: 'POST',
                    url: "/editortable",
                    data: data1,
                    success: function (res) {
                        if (res.Modification) {
                            $(".tdValuei").eq(0).remove();
                            show('修改成功');
                            var result = res.result, k;
                            for (var i in result) {
                                k = -2;
                                for (var j in result[i]) {
                                    k++;
                                    if (j === "id") continue;
                                    $("#table2 tbody tr").eq(i).find("td").eq(k).text(result[i][j]);
                                }
                            }

                            return;
                        }
                        alert("数据修改失败,请输入正确的格式");
                        document.getElementsByClassName("tdValuei")[0].focus();
                    }
                });
            } else {
                alert("你输入的不是数据类型请重新输入");
                document.getElementsByClassName("tdValuei")[0].focus();

            }
        });
    })
    //修改单元格的值
    var bodyscrollTop = storage.get("bodyscrollTop")
    var bodyscrollLeft = storage.get("bodyscrollLeft")
    // var setColor = storage.get("setColor").that;
    // console.log(setColor);
    // $(setColor).css({
    //     "background": "green"
    // })
    $("#table2_tableData").scrollTop(bodyscrollTop)
    $("#table2_tableData").scrollLeft(bodyscrollLeft)
    //右击选项卡功能begin
    var table2 = document.getElementById("table2");
    var tds = table2.getElementsByTagName("td");
    for (var i = 0; i < tds.length; i++) {
        (function (i) {
            tds[i].oncontextmenu = function (e) {
                var lineNumber = $(this).parent("tr").children("td").eq(0).text();
                var Num = $(this).parents("tr").find("td").index($(this));
                var field = $("#table2>thead>tr").eq(6).children("th").eq(Num).text();
                var tableName = decodeURIComponent(getUrlParms("tableName").split(",")[0]);//获取地址栏的参数并且编码转译
                var data1 = {
                    lineNumber: lineNumber,//行号
                    field: field,//字段
                    tableName: tableName//要修改的值是哪个部门
                };
                storage.set("CellInfo", data1)
                if (window.event) e = window.event;
                var mymenu = document.getElementById("menutab");
                mymenu.style.visibility = "visible";
                mymenu.style.left = e.clientX + 0 + "px";
                mymenu.style.top = e.clientY + 0 + "px";
                return false;
            }
        })(i)
    }
    document.onclick = function () {
        var mymenu = document.getElementById("menutab");
        mymenu.style.visibility = "hidden";
    }
    //右击选项卡功能end

});

// 右下角浮动导航
$(".wheel-button").wheelmenu({
    trigger: "click",
    animation: "fly",
    angle: [180, 270]
});

//全屏
function fullScreen(element) {
    if (document.documentElement.RequestFullScreen) {
        document.documentElement.RequestFullScreen();
    }
    //兼容火狐
    console.log(document.documentElement.mozRequestFullScreen)
    if (document.documentElement.mozRequestFullScreen) {
        document.documentElement.mozRequestFullScreen();
    }
    //兼容谷歌等可以webkitRequestFullScreen也可以webkitRequestFullscreen
    if (document.documentElement.webkitRequestFullScreen) {
        document.documentElement.webkitRequestFullScreen();
    }
    //兼容IE,只能写msRequestFullscreen
    if (document.documentElement.msRequestFullscreen) {
        document.documentElement.msRequestFullscreen();
    }
}